﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace CSM.Controllers
{
    public class GridViewUnit
    {
        /// <summary>
        /// 合并行(普通列)
        /// </summary>
        /// <param name=“gv”>所对应的GridView对象</param>
        /// <param name=“columnIndex”>所对应要合并的列的索引</param>
        public  void UnitCell(GridView gv, int columnIndex)
        {
            int i;
            string lastType="";
            int lastCell=0;
            System.Drawing.Color color1 = System.Drawing.Color.FromArgb(255, 255, 255);
            System.Drawing.Color color2 = System.Drawing.Color.FromArgb(242, 242, 242);


            if (gv.Rows.Count > 0)
            {
                lastType = gv.Rows[0].Cells[columnIndex].Text;
                gv.Rows[0].BackColor = color1;
                gv.Rows[0].Cells[columnIndex].RowSpan = 1;
                lastCell = 0;
            }
            for (i = 1; i < gv.Rows.Count; i++)
            {
                if (gv.Rows[i].Cells[columnIndex].Text == lastType)
                {
                    gv.Rows[i].Cells[columnIndex].Visible = false;
                    gv.Rows[i].BackColor = gv.Rows[i - 1].BackColor == color1 ? color1 : color2;
                    gv.Rows[lastCell].Cells[columnIndex].RowSpan++;
                }
                else
                {
                    lastType = gv.Rows[i].Cells[columnIndex].Text;
                    lastCell = i;
                    gv.Rows[i].BackColor = gv.Rows[i - 1].BackColor == color1 ? color2 : color1;
                    gv.Rows[i].Cells[columnIndex].RowSpan = 1;
                }
            }
        }
        /// <summary>
    /// 合并行(模板列)
    /// </summary>
    /// <param name=“gv”>所对应的GridView对象</param>
    /// <param name=“columnIndex”>所对应要合并的列的索引</param>   
    /// <param name=“lblName”>模板列里面Lable的Id</param>
    public  void UnitCell(GridView gv, int columnIndex, string lblName)
    {
        int i;
        string lastType="";
        int lastCell=0;
        if (gv.Rows.Count > 0)
        {
            lastType = (gv.Rows[0].Cells[columnIndex].FindControl(lblName) as Label).Text;
            gv.Rows[0].Cells[columnIndex].RowSpan = 1;
            lastCell = 0;
            for (i = 1; i < gv.Rows.Count; i++)
            {
                if ((gv.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text == lastType)
                {
                    gv.Rows[i].Cells[columnIndex].Visible = false;
                    gv.Rows[lastCell].Cells[columnIndex].RowSpan++;
                }
                else
                {
                    lastType = (gv.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text;
                    lastCell = i;
                    gv.Rows[i].Cells[columnIndex].RowSpan = 1;
                }
            }
        }
    }

    }
}
